<% layout('/layouts/default.html', {title: '通关缴税查询', libs: ['validate','fileupload','dataGrid']}){ %>

<div class="main-content">
    <div class="box box-main">
        <div class="box-header with-border">
            <div class="box-title">
                <i class="fa icon-note"></i> ${text('通关缴税批量选择生成')}
            </div>
            <div class="box-tools pull-right">
                <button type="button" class="btn btn-sm btn-default"  onclick="submitTab()">${text('提 交')}</button>
            </div>
        </div>
        <#form:form id="inputForm" model="${taxClearance}" action="${ctx}/financial/taxclearance/taxClearance/savePL" method="post" class="form-horizontal">
        <div class="box-body">
            <div class="form-unit">${text('基本信息')}</div>
            <#form:hidden path="taxClearanceId"/>
            <#form:hidden path="taxClearanceCode"/>
            <div class="row">
                <div class="col-xs-6">
                    <div class="form-group " >
                        <label class="control-label col-sm-4" title="">
                            <span class="required">*</span> ${text('客户')}：<i class="fa icon-question hide"></i></label>
                        <div class="col-sm-8">
                            <#form:select path="customerId" items="${customerList}" itemLabel="customerName"
                            itemValue="customerId" blankOption="true" class="form-control required" onchange="customerChange()"/>
                        </div>
                    </div>
                </div>
                <div class="col-xs-6">
                    <div class="form-group">
                        <label class="control-label col-sm-4" title="">
                            <span class="required">*</span> ${text('服务订单')}：<i class="fa icon-question hide"></i></label>
                        <div class="col-sm-8">
                            <#form:select path="serviceOrderId"  items="${serviceOrderList}" itemLabel="serviceOrderCode"
                            itemValue="serviceOrderId" blankOption="true" class="form-control required" onchange="serviceOrderChange()"/>
                        </div>
                    </div>
                </div>
                <div class="col-xs-6">
                    <div class="form-group">
                        <label class="control-label col-sm-4" title="">
                            <span class="required">*</span> ${text('国内资方')}：<i class="fa icon-question hide"></i></label>
                        <div class="col-sm-8">
                            <div class="control-inline">
                                <#form:select path="investorId" items="${gnzfList}" itemLabel="supplierName" itemValue="supplierId"
                                class="form-control required" blankOption="true" readonly="true" onchange="customsBrokerChange()"/>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="col-xs-6">
                    <div class="form-group">
                        <label class="control-label col-sm-4" title="">
                            <span class="required">*</span> ${text('报关行')}：<i class="fa icon-question hide"></i></label>
                        <div class="col-sm-8">
                            <#form:select path="customsBroker" items="${customsBrokerList}" itemLabel="supplierName" itemValue="supplierId"
                            class="form-control required" blankOption="true" readonly="true"/>
                        </div>
                    </div>
                </div>
                <div class="col-xs-12">
                    <div style="text-align:center;width:100%;">
                        <div>
                            <button type="button" class="btn btn-sm btn-default" id="btnSearch" onclick="searchTab1()">${text('查 询')}</button>
                            <button type="button" class="btn btn-sm btn-default" id="btnReset" onclick="resetTab1()">${text('重 置')}</button>
                        </div>
                    </div>
                </div>
            </div>
            <h4 class="form-unit">${text('车辆信息')}</h4>
            <div class="ml10 mr10">
                <table id="carInfoDataGrid"></table>
            </div>
            <h4 class="form-unit">${text('垫税信息-缴税信息')}</h4>
            <div class="row">
                <div class="col-xs-6">
                    <div class="form-group">
                        <label class="control-label col-sm-4" title="">
                            <span class="required">*</span> ${text('缴税方式')}：<i class="fa icon-question hide"></i></label>
                        <div class="col-sm-8">
                            <#form:select path="taxPaymentMethod" dictType="tax_payment_method" blankOption="true" class="form-control" onchange="taxPaymentMethodChange()"/>
                        </div>
                    </div>
                </div>
                <div class="col-xs-6">
                    <div class="form-group">
                        <label class="control-label col-sm-4" title="">
                            <span class="required">*</span> ${text('缴税日期')}：<i class="fa icon-question hide"></i></label>
                        <div class="col-sm-8">
                            <#form:input path="taxPaymentDate" readonly="true" maxlength="20" class="form-control laydate required"
                            dataFormat="date" data-type="date" data-format="yyyy-MM-dd" data-done="padTaxModeChange()"/>
                        </div>
                    </div>
                </div>
            </div>
            <div class="row">
                <div class="col-xs-6">
                    <div class="form-group">
                        <label class="control-label col-sm-4" title="">
                            <span class="required hide">*</span> ${text('垫方垫税方案')}：<i class="fa icon-question hide"></i></label>
                        <div class="col-sm-8">
                            <#form:select path="padTaxMode" itemLabel="name" itemValue="id"
                            class="form-control" blankOption="true" onchange="padTaxModeChange()"/>
                        </div>
                    </div>
                </div>
            </div>
            <div class="row">
                <div class="col-xs-6">
                    <div class="form-group">
                        <label class="control-label col-sm-4" title="">
                            <span class="required hide">*</span> ${text('代垫金额(RMB)')}：<i class="fa icon-question hide"></i></label>
                        <div class="col-sm-8">
                            <#form:input path="prepaidAmount" class="form-control required number"/>
                        </div>
                    </div>
                </div>
                <div class="col-xs-6">
                    <div class="form-group">
                        <label class="control-label col-sm-4" title="">
                            <span class="required hide">*</span> ${text('代收代付金额(RMB)')}：<i class="fa icon-question hide"></i></label>
                        <div class="col-sm-8">
                            <#form:input path="amount" class="form-control required number" />
                        </div>
                    </div>
                </div>
            </div>
            <div class="row">
                <div class="col-xs-12">
                    <div class="form-group">
                        <label class="control-label col-sm-2" title="">
                            <span class="required hide">*</span> ${text('垫税说明')}：<i class="fa icon-question hide"></i></label>
                        <div class="col-sm-10">
                            <#form:textarea path="padTaxMemo" rows="4" maxlength="255" class="form-control" />
                        </div>
                    </div>
                </div>
            </div>
            <h4 class="form-unit">${text('状态')}</h4>
            <div class="row">
                <div class="col-xs-6">
                    <div class="form-group">
                        <label class="control-label col-sm-4" title="">
                            <span class="required hide">*</span> ${text('状态')}：<i class="fa icon-question hide"></i></label>
                        <div class="col-sm-8">
                            <#form:select path="status1" dictType="tax_clearance_status" blankOption="true" class="form-control"/>
                        </div>
                    </div>
                </div>
            </div>
            <h4 class="form-unit">${text('垫税信息-资方')}</h4>
            <div class="ml10 mr10">
                <table id="taxClearanceDetailDataGrid1"></table>
            </div>
            <h4 class="form-unit">${text('垫税信息-客户')}</h4>
            <div class="ml10 mr10">
                <table id="taxClearanceDetailDataGrid2"></table>
            </div>
            <h4 class="form-unit">${text('附件信息')}</h4>
            <div class="ml10 mr10">
                <div class="row">
                    <div class="col-xs-12">
                        <div class="form-group">
                            <label class="control-label col-sm-2">
                                <span class="required hide">*</span> ${text('图片上传')}：</label>
                            <div class="col-sm-10" id="pic">
                                <#form:fileupload id="uploadImage" bizKey="${taxClearance1.id}" bizType="taxClearance_image"
                                uploadType="image" class="" readonly="false" preview="false"/>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="row">
                    <div class="col-xs-12">
                        <div class="form-group">
                            <label class="control-label col-sm-2">
                                <span class="required hide">*</span> ${text('附件上传')}：</label>
                            <div class="col-sm-10">
                                <#form:fileupload id="uploadFile" bizKey="${taxClearance1.id}" bizType="taxClearance_file"
                                uploadType="all" class="" readonly="false" preview="false"/>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="box-footer">
            <div class="row">
                <div class="col-sm-offset-2 col-sm-10">
                    <button type="button" class="btn btn-sm btn-default" id="btnCancel" onclick="js.closeCurrentTabPage()"><i class="fa fa-reply-all"></i> ${text('关 闭')}</button>
                </div>
            </div>
        </div>
    </#form:form>
</div>
</div>
<% } %>
<script>
    // 初始化DataGrid对象
    $('#carInfoDataGrid').dataGrid({
        datatype: "local", // 设置本地数据
        autoGridHeight: function(){return 'auto'}, // 设置自动高度
        columnModel: [
            {header:'状态', name:'status', editable:false, hidden:true},
            {header:'类型', name:'type', editable:false, hidden:true},
            {header:'主键', name:'taxClearanceDetailId', editable:false, hidden:true},
            {header:'${text("勾选")}', name:'customsDeclarationDetailId', width:60, editable:true, edittype:'checkbox'},
            {header:'${text("车架号")}', name:'frameNumber', width:120, editable:false, edittype:'text', editoptions:{'class':'form-control'}},
            {header:'${text("车辆id")}', name:'carId', width:150, editable:false, hidden:true},
            {header:'${text("车辆名称")}', name:'carName', width:150, editable:false, edittype:'text', editoptions:{'class':'form-control'}},
            {header:'${text("版型")}', name:'area', width:150,
                formatter: function(val, obj, row, act){
                    return js.getDictLabel(${@DictUtils.getDictListJson('car_quyu')}, val, '${text("")}', true);
                }
            },
            {header:'${text("已生成通关缴税")}', name:'submitStatus', width:120, editable:false, edittype:'text', editoptions:{'class':'form-control'}},
            {header:'${text("备注")}', name:'bgRemark', width:150, editable:false, edittype:'text', editoptions:{'class':'form-control'}}
        ],
        editGridInputFormListName: 'carInfoDataGrid', // 提交的数据列表名
        //showCheckbox: true,
        // 加载成功后执行事件
        ajaxSuccess: function(data){
        }
    });

    //初始化通关缴税明细DataGrid对象
    $("#taxClearanceDetailDataGrid1").dataGrid({

        data: ${toJson(taxClearance.taxClearanceDetailList1)},
        datatype: "local", // 设置本地数据
        autoGridHeight: function(){return 'auto'}, // 设置自动高度

        // 设置数据表格列
        columnModel: [
            {header:'状态', name:'status', editable:false, hidden:true},
            {header:'类型', name:'type', editable:false, hidden:true},
            {header:'主键', name:'taxClearanceDetailId', editable:false, hidden:true},
            {header:'${text("开始天数")}', name:'startDays', width:150, editable:false, edittype:'text', editoptions:{'class':'form-control digits','readonly':'true'}},
            {header:'${text("结束天数")}', name:'endDays', width:150, editable:false, edittype:'text', editoptions:{'class':'form-control digits','readonly':'true'}},
            {header:'${text("开始日期")}', name:'startDate', width:150,
                formatter:'date', formatoptions:{srcformat:'Y-m-d H:i:s',newformat:'Y-m-d'},
                editable:false, edittype:'text', editoptions:{'class':'form-control', 'readonly':'true',
                    dataInit: function(element){
                        laydate.render({elem:element, type:'date', format:'yyyy-MM-dd'});
                    }
                }
            },
            {header:'${text("结束日期")}', name:'endDate', width:150,
                formatter:'date', formatoptions:{srcformat:'Y-m-d H:i:s',newformat:'Y-m-d'},
                editable:false, edittype:'text', editoptions:{'class':'form-control', 'readonly':'true',
                    dataInit: function(element){
                        laydate.render({elem:element, type:'date', format:'yyyy-MM-dd'});
                    }
                }
            },
            {header:'${text("保证金比例")}', name:'marginRatio', width:150, editable:false, edittype:'text', editoptions:{'class':'form-control number','readonly':'true'}},
            {header:'${text("保证金金额")}', name:'marginAmount', width:150, editable:false, edittype:'text', editoptions:{'class':'form-control number','readonly':'true'}},
            {header:'${text("利率")}', name:'interestRate', width:150, editable:false, edittype:'text', editoptions:{'maxlength':'10', 'class':'form-control','readonly':'true'}},
            {header:'${text("备注")}', name:'remark', width:150, editable:false, edittype:'text', editoptions:{'maxlength':'255', 'class':'form-control','readonly':'true'}},
        ],

        // 加载成功后执行事件
        ajaxSuccess: function(data){

        }
    });

    //初始化通关缴税明细DataGrid对象
    $("#taxClearanceDetailDataGrid2").dataGrid({

        data: ${toJson(taxClearance.taxClearanceDetailList2)},
        datatype: "local", // 设置本地数据
        autoGridHeight: function(){return 'auto'}, // 设置自动高度

        // 设置数据表格列
        columnModel: [
            {header:'状态', name:'status', editable:false, hidden:true},
            {header:'类型', name:'type', editable:false, hidden:true},
            {header:'主键', name:'taxClearanceDetailId', editable:false, hidden:true},
            {header:'${text("开始天数")}', name:'startDays', width:150, editable:false, edittype:'text', editoptions:{'class':'form-control digits','readonly':'true'}},
            {header:'${text("结束天数")}', name:'endDays', width:150, editable:false, edittype:'text', editoptions:{'class':'form-control digits','readonly':'true'}},
            {header:'${text("开始日期")}', name:'startDate', width:150,
                formatter:'date', formatoptions:{srcformat:'Y-m-d H:i:s',newformat:'Y-m-d'},
                editable:false, edittype:'text', editoptions:{'class':'form-control', 'readonly':'true',
                    dataInit: function(element){
                        laydate.render({elem:element, type:'date', format:'yyyy-MM-dd'});
                    }
                }
            },
            {header:'${text("结束日期")}', name:'endDate', width:150,
                formatter:'date', formatoptions:{srcformat:'Y-m-d H:i:s',newformat:'Y-m-d'},
                editable:false, edittype:'text', editoptions:{'class':'form-control', 'readonly':'true',
                    dataInit: function(element){
                        laydate.render({elem:element, type:'date', format:'yyyy-MM-dd'});
                    }
                }
            },
            {header:'${text("保证金比例")}', name:'marginRatio', width:150, editable:false, edittype:'text', editoptions:{'class':'form-control number','readonly':'true'}},
            {header:'${text("保证金金额")}', name:'marginAmount', width:150, editable:false, edittype:'text', editoptions:{'class':'form-control number','readonly':'true'}},
            {header:'${text("利率")}', name:'interestRate', width:150, editable:false, edittype:'text', editoptions:{'maxlength':'10', 'class':'form-control','readonly':'true'}},
            {header:'${text("备注")}', name:'remark', width:150, editable:false, edittype:'text', editoptions:{'maxlength':'255', 'class':'form-control','readonly':'true'}}
        ],

        // 编辑表格参数
        editGrid: true,				// 是否是编辑表格
        editGridInitRowNum: 0,		// 编辑表格的初始化新增行数
        editGridAddRowInitData: {taxClearanceDetailId: '', status: Global.STATUS_NORMAL},	// 新增行的时候初始化的数据

        // 编辑表格的提交数据参数
        editGridInputFormListName: 'taxClearanceDetailList2', // 提交的数据列表名
        editGridInputFormListAttrs: 'status,taxClearanceDetailId,startDays,endDays,startDate,endDate,marginAmount,interestRate,interest,remark,type,taxClearanceId.taxClearanceId,', // 提交数据列表的属性字段

        // 加载成功后执行事件
        ajaxSuccess: function(data){
        }
    });
</script>
<script>
    function customerChange() {
        $.ajax({
            type: "POST",
            url: "${ctx}/financial/taxclearance/taxClearance/getServiceOrderList",
            data: {"customerId": $("#customerId").val()},
            success: function (data) {
                $("#serviceOrderId").empty();
                if (data.length > 0) {
                    for (let i = 0; i < data.length; i++) {
                        let partId = data[i].serviceOrderId;
                        let partName = data[i].serviceOrder.serviceOrderCode;
                        let option = $("<option>").attr({
                            "value" : partId
                        }).text(partName);
                        $("#serviceOrderId").append(option);
                    }
                    $("#serviceOrderId option:first").prop("selected", 'selected');
                    $("#serviceOrderId").change();
                }
                if(data == ""){
                    js.showErrorMessage("该客户没有服务订单！");
                }
            }
        });
    }

    function serviceOrderChange() {
        $.ajax({
            type: "POST",
            url: "${ctx}/financial/taxclearance/taxClearance/getGnzfList",
            data: {"serviceOrderId": $("#serviceOrderId").val()},
            success: function (data) {
                $("#investorId").empty();
                if (data.length > 0) {
                    for (let i = 0; i < data.length; i++) {
                        let partId = data[i].inSupplier.supplierId;
                        let partName = data[i].inSupplier.supplierName;
                        let option = $("<option>").attr({
                            "value" : partId
                        }).text(partName);
                        $("#investorId").append(option);
                    }
                    $("#investorId option:first").prop("selected", 'selected');
                    $("#investorId").change();
                }
                if(data == ""){
                    js.showErrorMessage("该服务订单没有国内资方！");
                }
            }
        });
    }

    function customsBrokerChange() {
        $.ajax({
            type: "POST",
            url: "${ctx}/financial/taxclearance/taxClearance/getCustomBrokerList",
            data: {"serviceOrderId": $("#serviceOrderId").val()},
            success: function (data) {
                $("#customsBroker").empty();
                if (data.length > 0) {
                    for (let i = 0; i < data.length; i++) {
                        let partId = data[i].customsBroker;
                        let partName = data[i].supplierName;
                        let option = $("<option>").attr({
                            "value" : partId
                        }).text(partName);
                        $("#customsBroker").append(option);
                    }
                    $("#customsBroker option:first").prop("selected", 'selected');
                    $("#customsBroker").change();
                }
                if(data == ""){
                    js.showErrorMessage("该服务订单没有报关行！");
                }
            }
        });
    }

    function searchTab1(){
        let customerId = $("#customerId").val();
        let serviceOrderId = $("#serviceOrderId").val();
        let investorId = $("#investorId").val();
        let customsBroker = $("#customsBroker").val();
        if(customerId == null||customerId == ''||serviceOrderId == null||serviceOrderId == ''||investorId == null||investorId == ''||customsBroker == null||customsBroker == ''){
            js.showErrorMessage("查询条件不全！");
        }
        let data ={"customerId":customerId,"serviceOrderId":serviceOrderId,"investorId":investorId,"customsBroker":customsBroker}
        $.ajax({
            type: "post",
            url: "${ctx}/financial/taxclearance/taxClearance/getCarInfoList",
            data: data,
            dataType: "json",
            success: function(data){
                if(data == null){
                    js.showMessage("请确认前置单据是否正确");
                    return
                }
                var ids1 = $("#carInfoDataGrid").getDataIDs();
                for (var i = 0; i < ids1.length ; i++) {
                    $('#carInfoDataGrid').dataGrid('delRowData', ids1[i]);
                }
                for(let i=0;i<data.length;i++) {
                    $("#carInfoDataGrid").jqGrid("addRow", {
                        position: "last",
                        addRowParams: {keys: false, focusField: true, selected:false},
                        initdata: {
                            frameNumber: data[i].frameNumber,
                            carId:data[i].carId,
                            carName: data[i].carName,
                            area: data[i].area,
                            submitStatus: data[i].submitStatus,
                            remark: data[i].remark,
                            taxClearanceDetailId:data[i].area
                        }
                    });
                }
                let rowDatas = $("#carInfoDataGrid").jqGrid("getRowData");
                let rowIds = $('#carInfoDataGrid').dataGrid('getDataIDs');
                for (let i = 1; i <= rowDatas.length; i++) {
                    let rowData = rowDatas[i-1];
                    let rowId = rowIds[i-1];
                    let checkBox = $("input[rowid='"+rowId+"']");
                    let submitStatus = rowData.submitStatus;
                    if(submitStatus == '已生成'){
                        $(checkBox).prop("disabled",true);
                    }
                }
                if (typeof window.webuploaderRefresh == 'function'){
                    window.webuploaderRefresh();
                }
            }
        });
    }

    function taxPaymentMethodChange(){
        if($("#taxPaymentMethod").val() == "1" || $("#taxPaymentMethod").val() == 1) {
            $("#padTaxMode").val("");
            $("#prepaidAmount").val("");
            $("#padTaxMemo").val("");
            $("#padTaxMode").prop("disabled",true);
            $("#prepaidAmount").prop("readonly",true);
            $("#padTaxMemo").prop("readonly",true);
        }else{
            $("#padTaxMode").prop("disabled",false);
            $("#prepaidAmount").prop("readonly",false);
            $("#padTaxMemo").prop("readonly",false);
        }

        if($("#taxPaymentMethod").val() != "1") {
            let investorId = $("#investorId").val();
            if (investorId == null || investorId == '') {
                js.showMessage("客户国内资方不存在");
                return
            }
            $.ajax({
                type: "post",
                async: true,
                url: "${ctx}/financial/taxclearance/taxClearance/getList",
                data: {
                    'supplierId': $("#investorId").val(),
                    'taxPaymentMethod': $("#taxPaymentMethod").val()
                },
                dataType: "json",
                success: function (msg) {
                    $("#padTaxMode").empty();
                    if (msg.length > 0) {
                        for (let i = 0; i < msg.length; i++) {
                            let partId = msg[i].investorFinancialInfoId;
                            let partName = msg[i].groupName;
                            let option = $("<option>").attr({
                                "value": partId
                            }).text(partName);
                            $("#padTaxMode").append(option);
                        }
                        $("#padTaxMode option:first").prop("selected", 'selected');
                        $("#padTaxMode").change();
                    }
                },
                error: function (json) {
                    $.jBox.alert("网络异常！");
                }
            });
        }else{
            $("#padTaxMode").empty();
        }
    }

    function resetTab1(){
        //$("#customerId").val("").trigger('change');
        //$("#serviceOrderId").val("").trigger('change');
        //$("#investorId").val("").trigger('change');
        //$("#customsBroker").val("").trigger('change');
        $('#carInfoDataGrid').jqGrid('clearGridData');
    }


    function padTaxModeChange() {
        if ($("#padTaxMode").val() != "" && $("#taxPaymentMethod").val() != "1") {
            $.ajax({
                type: "POST",
                url: "${ctx}/financial/taxclearance/taxClearance/getDetailList",
                data: {
                    "id": $("#padTaxMode").val()
                },
                success: function (data) {
                    // 资方
                    let ids1 = $('#taxClearanceDetailDataGrid1').dataGrid('getDataIDs');
                    for (let i = 0; i < ids1.length ; i++) {
                        $('#taxClearanceDetailDataGrid1').dataGrid('delRowData', ids1[i]);
                    }
                    for(let i=0; i<data.padTaxInterestList1.length; i++){
                        if(data.padTaxInterestList1[i].stageType == "1"){
                            $("#taxClearanceDetailDataGrid1").jqGrid("addRow", {
                                position: "last",
                                addRowParams: {keys: false, focusField: true},
                                initdata: {id: "",
                                    startDays: data.padTaxInterestList1[i].startDays,
                                    endDays: data.padTaxInterestList1[i].endDays,
                                    startDate: js.formatDate(js.addDate(js.parseDate($("#taxPaymentDate").val()), data.padTaxInterestList1[i].startDays), 'yyyy-MM-dd'),
                                    endDate:  js.formatDate(js.addDate(js.parseDate($("#taxPaymentDate").val()), data.padTaxInterestList1[i].endDays), 'yyyy-MM-dd'),
                                    marginRatio:data.padTaxInterestList1[i].investorMarginRatio,
                                    marginAmount: Number(data.padTaxInterestList1[i].investorMarginRatio)*Number($("#prepaidAmount").val()),
                                    interestRate:  data.padTaxInterestList1[i].investorInterestRatio,
                                    type:1
                                }
                            });
                        }
                    }
                    // 客户
                    let ids2 = $('#taxClearanceDetailDataGrid2').dataGrid('getDataIDs');
                    for (let i = 0; i < ids2.length ; i++) {
                        $('#taxClearanceDetailDataGrid2').dataGrid('delRowData', ids2[i]);
                    }
                    for(let i=0; i<data.padTaxInterestList1.length; i++){
                        if(data.padTaxInterestList1[i].stageType == "2"){
                            $("#taxClearanceDetailDataGrid2").jqGrid("addRow", {
                                position: "last",
                                addRowParams: {keys: false, focusField: true},
                                initdata: {id: "",
                                    startDays: data.padTaxInterestList1[i].startDays,
                                    endDays: data.padTaxInterestList1[i].endDays,
                                    startDate: js.formatDate(js.addDate(js.parseDate($("#taxPaymentDate").val()), data.padTaxInterestList1[i].startDays), 'yyyy-MM-dd'),
                                    endDate:  js.formatDate(js.addDate(js.parseDate($("#taxPaymentDate").val()), data.padTaxInterestList1[i].endDays), 'yyyy-MM-dd'),
                                    marginRatio: data.padTaxInterestList1[i].customerMarginRatio,
                                    marginAmount: Number(data.padTaxInterestList1[i].customerMarginRatio)*Number($("#prepaidAmount").val()),
                                    interestRate:  data.padTaxInterestList1[i].customerInterestRatio,
                                    type:2
                                }
                            });
                        }

                    }
                    if (typeof window.webuploaderRefresh == 'function'){
                        window.webuploaderRefresh();
                    }
                }
            });
        } else {
        }
    }

    $("#prepaidAmount").change(function () {
        // 资方
        let zfIds = $('#taxClearanceDetailDataGrid1').dataGrid('getDataIDs');
        for (let i = 0; i < zfIds.length ; i++) {
            $('#taxClearanceDetailDataGrid1').dataGrid('saveRow', zfIds[i], true);
            let ratio = $("#taxClearanceDetailDataGrid1").jqGrid("getCell", zfIds[i], "marginRatio");
            $("#taxClearanceDetailDataGrid1").jqGrid('setCell', zfIds[i], "marginAmount", Number(ratio)* Number($(this).val()));
            $('#taxClearanceDetailDataGrid1').dataGrid('editRow', zfIds[i]);
        }

        // 客户
        let khIds = $('#taxClearanceDetailDataGrid2').dataGrid('getDataIDs');
        for (let i = 0; i < khIds.length ; i++) {
            $('#taxClearanceDetailDataGrid2').dataGrid('saveRow', khIds[i], true);
            let ratio = $("#taxClearanceDetailDataGrid2").jqGrid("getCell", khIds[i], "marginRatio");
            $("#taxClearanceDetailDataGrid2").jqGrid('setCell', khIds[i], "marginAmount", Number(ratio)* Number($(this).val()));
            $('#taxClearanceDetailDataGrid2').dataGrid('editRow', khIds[i]);
        }
    });

    function submitTab() {
        //基本信息
        let customerId = $("#customerId").val();
        let serviceOrderId = $("#serviceOrderId").val();
        let investorId = $("#investorId").val();
        let customsBroker = $("#customsBroker").val();
        if(customerId == null||customerId == ''||serviceOrderId == null||serviceOrderId == ''||investorId == null||investorId == ''||customsBroker == null||customsBroker == ''){
            js.showErrorMessage("基本信息不完整！");
        }

        //车辆信息
        let ids = $('#carInfoDataGrid').dataGrid('getRowData');
        let rowIds = $('#carInfoDataGrid').jqGrid('getDataIDs');
        let carInfoArr = [];
        for (let i = 0; i < ids.length ; i++) {
            let curChk = $("#" + rowIds[i]).find(":checkbox");
            let st = curChk[0].checked;
            carInfoArr.push({
                "customsDeclarationDetailId": st+"",
                "frameNumber": ids[i].frameNumber,
                "carId": ids[i].carId,
                "area": ids[i].taxClearanceDetailId,
                "model": ids[i].submitStatus
            });
        }
        if(carInfoArr == null){
            js.showErrorMessage("车辆信息未生成！");
        }

        //缴税信息
        let taxPaymentMethod = $("#taxPaymentMethod").val();
        let taxPaymentDate = $("#taxPaymentDate").val();
        let padTaxMode = $("#padTaxMode").val();
        let prepaidAmount = $("#prepaidAmount").val();
        let amount = $("#amount").val();
        let padTaxMemo = $("#padTaxMemo").val();

        if(taxPaymentMethod == null || taxPaymentMethod == '' || taxPaymentDate == null || taxPaymentDate == '' ){
            js.showErrorMessage("缴税信息未填完整！");
        }

        //状态
        let status1 = $("#status1").val();
        if(status1 == null || status1 == ''){
            js.showErrorMessage("状态未选择！");
        }

        //垫税信息-资方
        let zids = $('#taxClearanceDetailDataGrid1').dataGrid('getRowData');
        let zInfoArr = [];
        for (let i = 0; i < zids.length ; i++) {
            $('#taxClearanceDetailDataGrid1').dataGrid('saveRow', zids[i], true);
            $("#taxClearanceDetailDataGrid1").dataGrid("restoreRow", zids[i]);
            let lineData = $('#taxClearanceDetailDataGrid1').dataGrid('getRowData', zids[i]);
            zInfoArr.push({
                "startDays": zids[i].startDays,
                "endDays": zids[i].endDays,
                "startDate": zids[i].startDate,
                "endDate": zids[i].endDate,
                "marginRatio":zids[i].marginRatio,
                "marginAmount":zids[i].marginAmount,
                "interestRate":zids[i].interestRate,
                "remark":zids[i].remark,
                "type":zids[i].type
            });
        }
        if(zInfoArr == null){
            js.showMessage("垫税信息-资方为空");
        }

        //垫税信息-客户
        let kids = $('#taxClearanceDetailDataGrid2').dataGrid('getRowData');
        let kInfoArr = [];
        for (let i = 0; i < kids.length ; i++) {
            $('#taxClearanceDetailDataGrid2').dataGrid('saveRow', kids[i], true);
            $("#taxClearanceDetailDataGrid2").dataGrid("restoreRow", kids[i]);
            let lineData = $('#taxClearanceDetailDataGrid2').dataGrid('getRowData', kids[i]);
            kInfoArr.push({
                "startDays": kids[i].startDays,
                "endDays": kids[i].endDays,
                "startDate": kids[i].startDate,
                "endDate": kids[i].endDate,
                "marginRatio":kids[i].marginRatio,
                "marginAmount":kids[i].marginAmount,
                "interestRate":kids[i].interestRate,
                "remark":kids[i].remark,
                "type":kids[i].type
            });
        }
        if(kInfoArr == null){
            js.showMessage("垫税信息-客户为空");
        }

        //附件上传id
        let bizKey = "${taxClearance1.id}";

        $.ajax({
            type: "POST",
            async: true,
            url : "${ctx}/financial/taxclearance/taxClearance/savePL",
            data: {
                    "customerId":customerId,
                    "serviceOrderId":serviceOrderId,
                    "investorId":investorId,
                    "customsBroker":customsBroker,
                    "taxPaymentMethod":taxPaymentMethod,
                    "taxPaymentDate":taxPaymentDate,
                    "padTaxMode":padTaxMode,
                    "prepaidAmount":prepaidAmount,
                    "amount":amount,
                    "padTaxMemo":padTaxMemo,
                    "status1":status1,
                    "bizKey":bizKey,
                    "taxClearanceDetailList1":JSON.stringify(zInfoArr),
                    "taxClearanceDetailList2":JSON.stringify(kInfoArr),
                    "carInfoList":JSON.stringify(carInfoArr)
                  },
            dataType:"json",
            success: function (result) {
                searchTab1();
                js.showMessage(result.message);
            }
        });
    }
</script>


